package com.huawei.appgallery.forum.user.userinfo.activity;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;
import com.huawei.appgallery.common.media.api.IMediaSelectProtocol;
import com.huawei.appgallery.common.media.api.IMediaSelectResult;
import com.huawei.appgallery.common.media.api.OriginalMediaBean;
import com.huawei.appgallery.forum.base.ui.ForumActivity;
import com.huawei.appgallery.foundation.account.bean.UserSession;
import com.huawei.hwphy2d.physical.util.BarrierParameters;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import huawei.widget.HwEditText;
import huawei.widget.HwTextView;
import java.io.File;
import java.util.List;
import o.azb;
import o.bqx;
import o.bqy;
import o.cbb.a;
import o.cbb.d;
import o.cbb.j;
import o.cca;
import o.ccd;
import o.ccf;
import o.ccg;
import o.cci;
import o.ccj;
import o.cck;
import o.ccl;
import o.cgv;
import o.dof;
import o.doo;
import o.epv;
import o.CollectCheck;
import o.equ;
import o.erz;
import o.evk;
import o.fho;
import o.fiu;
import o.fjb;
import o.fjc;

public class PersonalDataActivity extends ForumActivity implements cca, cci, ccd {
    private ProgressBar ʻॱ;
    private LinearLayout ʽॱ;
    private String ʾ;
    private TextView ʿ;
    private boolean ˈ = false;
    private boolean ˉ = false;
    private String ˊˊ;
    private File ˊˋ;
    private ccf ˊᐝ;
    private File ˋˊ;
    private Handler ˋˋ = new Handler(this) {
        final /* synthetic */ PersonalDataActivity ˊ;

        {
            this.ˊ = r1;
        }

        public void handleMessage(Message message) {
            if (message.what == 0) {
                this.ˊ.ˏ(false);
            } else if (message.what == 1) {
                this.ˊ.ʿ.setVisibility(0);
                this.ˊ.ᐝॱ.setTextColor(this.ˊ.getResources().getColor(o.cbb.c.ʽ));
            }
        }
    };
    private String ˋॱ;
    private InputFilter ˌ = new c(15, this);
    private ImageView ͺ;
    private String ॱˊ;
    private ImageView ॱˋ;
    private HwEditText ॱˎ;
    private TextView ᐝॱ;

    static class c implements InputFilter {
        private final int ˋ;
        private ccd ॱ;

        c(int i, ccd o_ccd) {
            this.ˋ = i;
            this.ॱ = o_ccd;
        }

        public CharSequence filter(CharSequence charSequence, int i, int i2, Spanned spanned, int i3, int i4) {
            int length = this.ˋ - (spanned.length() - (i4 - i3));
            if (length <= 0) {
                ॱ();
                return "";
            } else if (length >= i2 - i) {
                return null;
            } else {
                length += i;
                if (Character.isHighSurrogate(charSequence.charAt(length - 1))) {
                    length--;
                    if (length == i) {
                        return "";
                    }
                }
                ॱ();
                return charSequence.subSequence(i, length);
            }
        }

        private void ॱ() {
            if (this.ॱ != null) {
                this.ॱ.ᐝ();
            }
        }
    }

    class e implements TextWatcher {
        final /* synthetic */ PersonalDataActivity ˊ;

        private e(PersonalDataActivity personalDataActivity) {
            this.ˊ = personalDataActivity;
        }

        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        public void afterTextChanged(Editable editable) {
            this.ˊ.ॱˊ = editable.toString();
            int length = editable.length();
            this.ˊ.ᐝॱ.setText(this.ˊ.getResources().getString(j.ॱˎ, new Object[]{Integer.valueOf(length), Integer.valueOf(15)}));
            this.ˊ.ʿ.setVisibility(8);
            if (length < 15) {
                this.ˊ.ˉ = false;
            }
            this.ˊ.ˏ(length);
            if (this.ˊ.ॱˊ.length() <= 0 || TextUtils.isEmpty(this.ˊ.ॱˊ.trim())) {
                this.ˊ.ॱˋ.setAlpha(0.3f);
                this.ˊ.ʽॱ.setEnabled(false);
                return;
            }
            this.ˊ.ॱˋ.setAlpha(1.0f);
            this.ˊ.ʽॱ.setEnabled(true);
        }
    }

    public void onCreate(@Nullable Bundle bundle) {
        super.onCreate(bundle);
        cgv.ˋ(this, o.cbb.c.ॱ, o.cbb.c.ᐝ);
        ͺ();
        setContentView(d.ͺ);
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        ॱˊ();
        ᐝॱ();
        ʼॱ();
    }

    private void ॱˊ() {
        View view = (ViewGroup) findViewById(b.ﾞ);
        azb.ˏ(view);
        ((HwTextView) view.findViewById(b.ʽˊ)).setText(getResources().getString(j.ˏˏ));
        ((ImageView) view.findViewById(b.ʽᐝ)).setImageDrawable(getResources().getDrawable(a.ˊ));
        view.findViewById(b.ˎˏ).setOnClickListener(new OnClickListener(this) {
            final /* synthetic */ PersonalDataActivity ॱ;

            {
                this.ॱ = r1;
            }

            public void onClick(View view) {
                this.ॱ.ˈ = false;
                this.ॱ.finish();
            }
        });
        this.ॱˋ = (ImageView) view.findViewById(b.ॱʽ);
        this.ॱˋ.setImageDrawable(getResources().getDrawable(a.ॱॱ));
        this.ʽॱ = (LinearLayout) view.findViewById(b.ـ);
        this.ʽॱ.setOnClickListener(new OnClickListener(this) {
            final /* synthetic */ PersonalDataActivity ˋ;

            {
                this.ˋ = r1;
            }

            public void onClick(View view) {
                this.ˋ.ˋˊ();
            }
        });
        this.ʻॱ = (ProgressBar) view.findViewById(b.ʾॱ);
        if (TextUtils.isEmpty(this.ॱˊ)) {
            this.ॱˋ.setAlpha(Float.valueOf(azb.ˋ((Context) this, o.cbb.e.ˊ)).floatValue());
            this.ʽॱ.setEnabled(false);
            return;
        }
        this.ॱˋ.setAlpha(1.0f);
        this.ʽॱ.setEnabled(true);
    }

    private void ͺ() {
        this.ˋॱ = UserSession.getInstance().getHeadUrl();
        this.ॱˊ = UserSession.getInstance().getNickname();
        if (!dof.ॱ(this.ॱˊ) && this.ॱˊ.length() >= 15) {
            this.ॱˊ = this.ॱˊ.substring(0, 15);
        }
        if (this.ˊᐝ == null) {
            this.ˊᐝ = new ccf(this);
        }
    }

    private void ˏ(boolean z) {
        boolean z2;
        int i = 8;
        boolean z3 = true;
        this.ʻॱ.setVisibility(z ? 0 : 8);
        LinearLayout linearLayout = this.ʽॱ;
        if (!z) {
            i = 0;
        }
        linearLayout.setVisibility(i);
        ImageView imageView = this.ͺ;
        if (z) {
            z2 = false;
        } else {
            z2 = true;
        }
        imageView.setEnabled(z2);
        HwEditText hwEditText = this.ॱˎ;
        if (z) {
            z3 = false;
        }
        hwEditText.setEnabled(z3);
    }

    private void ᐝॱ() {
        azb.ˏ(findViewById(b.ﾟ));
        this.ͺ = (ImageView) findViewById(b.ͺॱ);
        ॱ(this.ͺ);
        this.ͺ.setOnClickListener(new OnClickListener(this) {
            final /* synthetic */ PersonalDataActivity ˎ;

            {
                this.ˎ = r1;
            }

            public void onClick(View view) {
                this.ˎ.ˊˊ();
            }
        });
        this.ॱˎ = (HwEditText) findViewById(b.ˑ);
        this.ᐝॱ = (TextView) findViewById(b.ˌ);
        this.ʿ = (TextView) findViewById(b.ˎˎ);
        this.ॱˎ.addTextChangedListener(new e());
        this.ॱˎ.setOnFocusChangeListener(new OnFocusChangeListener(this) {
            final /* synthetic */ PersonalDataActivity ˋ;

            {
                this.ˋ = r1;
            }

            public void onFocusChange(View view, boolean z) {
                this.ˋ.ˏ(this.ˋ.ॱˎ.getText().length());
                if (!z) {
                    this.ˋ.ˉ = false;
                }
            }
        });
        this.ॱˎ.setFilters(new InputFilter[]{this.ˌ});
    }

    private void ʼॱ() {
        if (!TextUtils.isEmpty(this.ˋॱ)) {
            equ.ˊ(this.ͺ, this.ˋॱ, "head_default_icon");
        }
        if (!TextUtils.isEmpty(this.ॱˊ)) {
            this.ॱˎ.setText(this.ॱˊ);
            this.ॱˎ.setSelection(this.ॱˊ.length());
        }
    }

    private void ॱ(View view) {
        LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
        int ˎ = (((int) (((double) epv.ˎ((Context) this)) / 2.3333333333333335d)) - getResources().getDimensionPixelSize(o.cbb.e.ͺ)) / 2;
        layoutParams.bottomMargin = ˎ;
        layoutParams.topMargin = ˎ;
        view.setLayoutParams(layoutParams);
    }

    private void ˋˊ() {
        if (doo.ˊ(bqy.ˋ().ˏ())) {
            String str = "";
            if (this.ˊˋ != null && this.ˊˋ.exists()) {
                str = this.ˊˋ.getPath();
            }
            if (this.ˊᐝ != null) {
                ˏ(true);
                this.ˊᐝ.ॱ(str, this.ॱˎ.getText().toString().trim());
                return;
            }
            return;
        }
        evk.ˏ(this, j.ͺॱ, 0).ˋ();
    }

    private void ˊˊ() {
        cck ˏ = cck.ˏ();
        ˏ.ˋ(new cck.d(this) {
            final /* synthetic */ PersonalDataActivity ˊ;

            {
                this.ˊ = r1;
            }

            public void ˏ(int i) {
                if (i == 0) {
                    if (!ccg.ॱ(this.ˊ, 21)) {
                        this.ˊ.ˉ();
                    }
                } else if (i == 1) {
                    this.ˊ.ˊᐝ();
                }
            }
        });
        ˏ.ˎ((Context) this);
    }

    public void onRequestPermissionsResult(int i, @NonNull String[] strArr, @NonNull int[] iArr) {
        ccg.ˊ((cci) this, i, strArr, iArr);
    }

    private void ˊᐝ() {
        fjb ˎ = fho.ˏ().ˋ("Media").ˎ("MediaSelect");
        IMediaSelectProtocol iMediaSelectProtocol = (IMediaSelectProtocol) ˎ.ˊ();
        iMediaSelectProtocol.setMediaType("image");
        iMediaSelectProtocol.setMaxSelectSize(1);
        iMediaSelectProtocol.setMaxSelectFileSize(10485760);
        iMediaSelectProtocol.setSelectForHeadImg(true);
        fiu.ˊ().ॱ((Context) this, ˎ, new fjc<IMediaSelectResult>(this) {
            final /* synthetic */ PersonalDataActivity ॱ;

            {
                this.ॱ = r1;
            }

            public /* synthetic */ void ˎ(int i, Object obj) {
                ˊ(i, (IMediaSelectResult) obj);
            }

            public void ˊ(int i, IMediaSelectResult iMediaSelectResult) {
                ((PersonalDataActivity) ˊ()).ˊ(i, iMediaSelectResult);
            }
        });
    }

    private void ˊ(int i, IMediaSelectResult iMediaSelectResult) {
        if (i == -1 && iMediaSelectResult != null) {
            try {
                List selectedMedias = iMediaSelectResult.getSelectedMedias();
                if (!CollectCheck.isEmpty(selectedMedias)) {
                    Uri fromFile = Uri.fromFile(new File(((OriginalMediaBean) selectedMedias.get(0)).ʽ()));
                    this.ˋˊ = ccl.ˊ(this, true);
                    if (this.ˋˊ == null) {
                        bqx.ˋ("PersonalDataActivity", "onResult, createTempUserIconFile error");
                    } else if (ccl.ˋ(this, fromFile, FileProvider.getUriForFile(this, ˊˋ(), this.ˋˊ))) {
                        ˋᐝ();
                    }
                }
            } catch (Exception e) {
                bqx.ॱ("PersonalDataActivity", "cast error");
            }
        }
    }

    private void ˉ() {
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        this.ˋˊ = ccl.ˊ(this, true);
        if (this.ˋˊ == null) {
            bqx.ˋ("PersonalDataActivity", "startCameraActivity, createTempUserIconFile error");
            return;
        }
        intent.setFlags(3);
        intent.putExtra("output", FileProvider.getUriForFile(this, ˊˋ(), this.ˋˊ));
        try {
            startActivityForResult(intent, 0);
        } catch (Throwable e) {
            bqx.ˊ("PersonalDataActivity", "start Camero Exception:", e);
        }
    }

    private String ˊˋ() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(bqy.ˋ().ˏ().getPackageName()).append(".forum.user.provider");
        return stringBuilder.toString();
    }

    public void ˎ(String str, String str2) {
        this.ˈ = true;
        this.ˋˋ.sendEmptyMessage(0);
        this.ʾ = str;
        this.ˊˊ = str2;
        finish();
    }

    public void ˎ(int i) {
        this.ˈ = false;
        this.ˋˋ.sendEmptyMessage(0);
        if (i == 70005003) {
            this.ˋˋ.sendEmptyMessage(1);
        }
    }

    public void finish() {
        int i;
        Intent intent = new Intent();
        if (this.ˈ) {
            intent.putExtra("headPic", TextUtils.isEmpty(this.ʾ) ? this.ˋॱ : this.ʾ);
            intent.putExtra("nickname", this.ˊˊ);
            i = -1;
        } else {
            i = 0;
        }
        setResult(i, intent);
        super.finish();
    }

    public void ˊ(int i, int i2) {
        if (i2 != 0) {
            return;
        }
        if (i == 21) {
            ˉ();
        } else if (i == 22) {
            ˊᐝ();
        } else if (i == 24) {
            ˎˎ();
        }
    }

    public void ᐝ() {
        this.ˉ = true;
        ˏ(this.ॱˎ.getText().length());
    }

    private void ˏ(int i) {
        if (this.ॱˎ.hasFocus()) {
            if (i >= 13) {
                this.ᐝॱ.setVisibility(0);
                this.ᐝॱ.setTextColor(getResources().getColor(o.cbb.c.ʽ));
            } else {
                this.ᐝॱ.setVisibility(8);
            }
            if (this.ˉ) {
                this.ᐝॱ.setTextColor(getResources().getColor(o.cbb.c.ॱॱ));
                ˌ();
                return;
            }
            return;
        }
        this.ᐝॱ.setVisibility(8);
    }

    private void ˌ() {
        AlphaAnimation alphaAnimation = new AlphaAnimation(BarrierParameters.RESTITUTION, 1.0f);
        alphaAnimation.setDuration(10);
        alphaAnimation.setRepeatCount(5);
        alphaAnimation.setRepeatMode(2);
    }

    private void ˋᐝ() {
        if (this.ˋˊ == null || !this.ˋˊ.exists()) {
            bqx.ˋ("PersonalDataActivity", "cropImage userIconTempFile null or not exists");
            return;
        }
        this.ˊˋ = ccl.ˊ(this, false);
        if (this.ˊˋ == null) {
            bqx.ˋ("PersonalDataActivity", "onResult, createUserIconFile error");
            return;
        }
        Intent intent = new Intent("com.android.camera.action.CROP");
        try {
            Uri uriForFile = FileProvider.getUriForFile(this, ˊˋ(), this.ˋˊ);
            Object uriForFile2 = FileProvider.getUriForFile(this, ˊˋ(), this.ˊˋ);
            ॱ(intent);
            intent.putExtra("crop", "true");
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            intent.putExtra("outputX", 1080);
            intent.putExtra("outputY", 1080);
            intent.putExtra("return-data", false);
            intent.addFlags(1);
            intent.addFlags(2);
            intent.setDataAndType(uriForFile, "image/*");
            intent.putExtra("output", uriForFile2);
            for (ResolveInfo resolveInfo : getPackageManager().queryIntentActivities(intent, WXMediaMessage.THUMB_LENGTH_LIMIT)) {
                grantUriPermission(resolveInfo.activityInfo.packageName, uriForFile2, 3);
            }
            intent.putExtra("outputFormat", CompressFormat.PNG.toString());
            startActivityForResult(intent, 2);
        } catch (Throwable e) {
            bqx.ˊ("PersonalDataActivity", "cropImage Exception", e);
        }
    }

    private void ॱ(Intent intent) {
        if (erz.ˏ(this, "com.android.gallery3d")) {
            intent.setPackage("com.android.gallery3d");
        } else if (erz.ˏ(this, "com.huawei.photos")) {
            intent.setPackage("com.huawei.photos");
        } else {
            bqx.ˋ("PersonalDataActivity", "setIntentPackage, not set intent package");
        }
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i2 != -1) {
            bqx.ˋ("PersonalDataActivity", "onActivityResult failed, requestCode:" + i + " ;resultCode:" + i2);
        } else if (i == 0) {
            ˋᐝ();
        } else if (i == 2 && !ccg.ॱ(this, 24)) {
            ˎˎ();
        }
    }

    private void ˍ() {
        if (!(this.ˋˊ == null || !this.ˋˊ.exists() || this.ˋˊ.delete())) {
            bqx.ˋ("PersonalDataActivity", "delete userIconTempFile failed");
        }
        if (this.ˊˋ != null && this.ˊˋ.exists() && !this.ˊˋ.delete()) {
            bqx.ˋ("PersonalDataActivity", "delete userIconFile failed");
        }
    }

    private void ˎˎ() {
        if (this.ˊˋ == null || !this.ˊˋ.exists()) {
            bqx.ˋ("PersonalDataActivity", "photoResult,photo is null");
        } else {
            ccj.ˎ(this.ͺ, this.ˊˋ.getAbsolutePath(), "head_default_icon");
        }
    }

    public void onDestroy() {
        ˍ();
        if (this.ˊᐝ != null) {
            this.ˊᐝ = null;
        }
        super.onDestroy();
    }
}
